/**
 * jquery fn vscroll 模拟滚动条
 * 结构:
 * <div id=''>
 * 		<div class="vscroll-content"></div>
 * 		<div class="vscroll-wrap">
 * 			<span class="vscroll-bar drag-div"></span>
 * 		</div>
 * </div>
 * @param
 * options = {
 * 		
 * }
 * @author  tanggf
 */
(function($){
	$.fn.vscroll = function(options){
		//default setting
		options = $.extend({
			
		}, options);
		
		this.each(function(){
			_get_vscroll_(this, options);
		});	
		
		return _get_vscroll_(this[0], options);
	};
	
	function _get_vscroll_(ele, options){
		var $this = $(ele);
		var obj = $this.data("_wt_vscroll_");
		if( ! obj){
			obj = new _Vscroll_(ele, options);
			$this.data("_wt_vscroll_", obj);
		}
		return obj;
	}
	
	function _Vscroll_(ele, options){
		this.$ele = $(ele);
		this.options = options;
		this._init_();
	}
	
	_Vscroll_.prototype = {
		_init_: function(){
			this.$content = this.$ele.children("div.vscroll-content");
			this.$content.css("position", "relative");
			this.$scrollWrap = this.$ele.children("div.vscroll-wrap");
			this.$scrollBar = this.$scrollWrap.children("span.vscroll-bar");
			this.maxheight = this.$ele.height();			
			this.wrapheight = this.$scrollWrap.height();
			
			var self = this;
			this.$scrollWrap.drag({
				axis: 'y',
				onmove: function(event){	
					self._scroll_();
				}
			});
			
			this.resize();
		},
		
		resize: function(){
			this.cheight = this.$content.height();					
			if(this.cheight > this.maxheight){
				this._cal_scroll_bar();
			}else{
				this.$scrollBar.height(0);
			}
		},
		
		_cal_scroll_bar: function(){
			var h = (this.maxheight * this.wrapheight) / this.cheight;			
			this.$scrollBar.height(Math.floor(h));			
		},
		
		_scroll_: function(){
			var top = parseInt(this.$scrollBar.css("top").replace("px", "")) || 0;
			var stop = (top * this.cheight) / this.wrapheight;		
			stop = Math.floor(stop);
			this.$content.css("top", -stop);
		}
	};
})(jQuery);